var  app = getApp();
let keys = '4UTBZ-PUWLX-UGS4R-TIK3C-WFL73-N4FWH';
const QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
const qqmapsdk = new QQMapWX({
  key:keys
});
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cityIndex:0,
        more:1,
        store:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that=this;
      
        var parent_id=options.parent_id;
        if(parent_id!=undefined){
            this.setData({
                parent_id:parent_id
              })
              wx.setStorageSync('parent_id',parent_id);
        }else{
            parent_id = wx.getStorageSync('parent_id');
        
        }
        var userInfo=wx.getStorageSync('userInfo');
        if(userInfo==""){
            wx.navigateTo({
              url: '../login/index?parent_id='+parent_id,
            })
        }
        
        wx.showShareMenu({
            menus: ['shareAppMessage', 'shareTimeline'],
            success(res) {
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          })  
                  
        that.setData({
            platform:app.globalData.platform,    
            userInfo:userInfo,
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.domain,
            uploadUrl:app.globalData.uploadUrl,
          })
          this.getIndex();
          this.getCity(1);
    },
    getIndex:function(){
        let that = this; 
        var userInfo=this.data.userInfo;
        var parent_id = wx.getStorageSync('parent_id');    
        wx.request({
          url: app.globalData.domain+'store/getIndex',
          data:{from:1,user_id:userInfo.id,parent_id:parent_id},
          success:res=>{
              console.log('my');
              console.log(res)
             if(res.statusCode==200){
                 let data=res.data;
                that.setData({
                shareTitle:data.shareTitle,
                shareLogo:data.shareLogo,
                shareIndex:data.shareIndex,
                backgroud:data.backgroud
                })
             }
          }
        })
     },
    getCity:function(page){
        let that=this;
        var userInfo=wx.getStorageSync('userInfo');
        var parent_id = wx.getStorageSync('parent_id');
        console.log('----------getCity parent_id-------');
        console.log(parent_id);
        console.log('----------getCity parent_id-------');
        var more=this.data.more;
        wx.request({        
            url: app.globalData.domain+'store/getCitys',
            data:{from:1,user_id:userInfo.id,parent_id:parent_id,page:page},
            success:res=>{
                console.log(res);
                if(res.statusCode==200){
                    let data=res.data;
                    //var city = Object.values(data.city); 
                    var city=[];
                    var citys=data.city;
                    citys.forEach(function(item,index){
                        city[item.id]=item.city;
                    });
                    var store=data.store;
                    store.forEach(function(item,index){
                        var marks=[];
                        marks.push({
                            latitude:item.lat,
                            longitude:item.lng,
                            iconPath: 'http://webapi.amap.com/theme/v1.3/markers/b/mark_bs.png',
                            id: 0,
                            width: 19,
                            height: 33,
                            name: item.name,                           
                            callout:{
                            content:item.name
                            }
                        });
                        store[index].markers=marks;
                    })
                    var stores=that.data.store;
                    var list=stores.concat(store);
                    
                    console.log(list);
                    that.setData({
                        citys:citys,
                        city:city,
                        store:list,
                        more:data.more,
                        page:data.page,             
                    })
                }else{
                   wx.showLoading({
                     title: '系统错误',
                   }) 
                }
            }
            })
    },
    call:function(e){
        var phone=e.currentTarget.dataset.phone;
        wx.makePhoneCall({
          phoneNumber: phone,
        })
  
    },
    gotoNavigation:function(e){
        console.log(e);
      var position=e.currentTarget.dataset.position;
      var lat=e.currentTarget.dataset.lat;
      var lnt=e.currentTarget.dataset.lnt;
      var address=e.currentTarget.dataset.address;
      wx.navigateTo({
         url: '../map/navigation?position='+position+"&latitude="+lat+"&longitude="+lnt+"&address="+address,
      })
    },
    goBack:function(){
        wx.navigateBack();
      },
      goShopcart:function(){
        wx.navigateTo({
          url: '../shopcart/index',
        })
    },
    goShopcart:function(){
        wx.navigateTo({
          url: '../shopcart/index',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    cityChange:function(e){
        //console.log(e)
        let that=this;
        var index=e.detail.value;
        var cityIndex=this.data.cityIndex;
        var citys=this.data.citys;
        var userInfo=wx.getStorageSync('userInfo');
        var city=citys[index].id;
        if(index>0){
            if(index!=cityIndex){
               // this.getArea()
               wx.request({
                url: app.globalData.domain+'store/getArea',
                data:{from:1,user_id:userInfo.id,city:city},
                success:res=>{
                    console.log(res);
                    if(res.statusCode==200){
                        let data=res.data;                
                        var area=[];
                        var areas=data.area;
                        console.log(areas);
                        areas.forEach(function(item,index){
                            console.log(item);
                            area[index]=item.city;
                        });
                        console.log(area);
                        var store=data.store;
                        store.forEach(function(item,index){
                            var marks=[];
                            marks.push({
                                latitude:item.lat,
                                longitude:item.lng,
                                iconPath: 'http://webapi.amap.com/theme/v1.3/markers/b/mark_bs.png',
                                id: 0,
                                width: 19,
                                height: 33,
                                name: item.name,                           
                                callout:{
                                content:item.name
                                }
                            });
                            store[index].markers=marks;
                        })
                        that.setData({
                            areas:areas,
                            area:area,
                            store:store                
                       
                        })
                    }else{
                       wx.showLoading({
                         title: '系统错误',
                       }) 
                    }
                }
               })
            }
        }
        this.setData({
            cityIndex:index,
            areaIndex:0
        })
    },
    areaChange:function(e){
        //console.log(e)
        let that=this;
        var index=e.detail.value;
        var areaIndex=this.data.areaIndex;
        var areas=this.data.areas;
        var userInfo=wx.getStorageSync('userInfo');
        var area=areas[index].id;
        if(index>0){
            if(index!=areaIndex){
               // this.getArea()
               wx.request({
                url: app.globalData.domain+'store/getStore',
                data:{from:1,user_id:userInfo.id,area:area},
                success:res=>{
                    console.log(res);
                    if(res.statusCode==200){
                        let data=res.data; 
                       console.log(data);
                        var store=[];
                        var stores=data.store;
                        console.log(stores);                     
                        console.log(area);
                        var store=data.store;
                        store.forEach(function(item,index){
                            var marks=[];
                            marks.push({
                                latitude:item.lat,
                                longitude:item.lng,
                                iconPath: 'http://webapi.amap.com/theme/v1.3/markers/b/mark_bs.png',
                                id: 0,
                                width: 19,
                                height: 33,
                                name: item.name,                           
                                callout:{
                                content:item.name
                                }
                            });
                            store[index].markers=marks;
                        })
                        that.setData({                            
                            store:store,
                            areaIndex:index
                       
                        })
                    }else{
                       wx.showLoading({
                         title: '系统错误',
                       }) 
                    }
                }
               })
            }
        }
        this.setData({
            areaIndex:index,
            storeIndex:0
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },
    onShareTimeline(){
        //console.log(e);
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareLogo;
        var userInfo=wx.getStorageSync('userInfo');

        var parent_id=userInfo.id;
         this.setData({
           share:true
         });
         console.log(shareLogo);
         console.log(title);
         return{
           title: title,                 
           query: 'parent_id='+parent_id,        
           imageUrl:shareLogo      
           }
       },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        var more=this.data.more;
        var page =this.data.page;
        if(more==1){
            page=parseInt(page)+1;
            this.getCity(page)
        }else{
            console.log('no more');
        } 
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage(e) {
        var userInfo=wx.getStorageSync('userInfo');
        var parent_id=userInfo.id;
        var that = this;
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareLogo;
   　　var shareObj = {
   　　　　title: title,    
   　　　　path: '/pages/index/index?parent_id='+parent_id,   
   　　　　imageUrl: shareLogo 
   　　　
   　　}
   　　// 来自页面内的按钮的转发
   　　if( e.from == 'button' ){
     　　　shareObj.title=title;
   　　　　shareObj.path = '/pages/index/index?     parent_id='+parent_id;
   　　}
   console.log(shareObj);
   　　// 返回shareObj
   　　return shareObj;
    }
})